import axios from "axios";

function myAxios(axiosConfig, headerObj) {
    return new Promise((resolve, reject) => {
        const service = axios.create({
            baseURL: 'http://49.234.63.243:8080', // 设置统一的请求前缀
            timeout: 10000, // 设置统一的超时时长
        });


        // 请求拦截
        service.interceptors.request.use(
            config => {
                if (headerObj !== 'undefined') {
                    for (let key in headerObj) {
                        config.headers[key] = headerObj[key]
                    }
                }
                return config;
            },
            error => {
                return Promise.reject(error);
            }
        );

        // 响应拦截
        service.interceptors.response.use(
            response => {
                return response;
            },
            error => {
                return Promise.reject(error);
            }
        );

        service(axiosConfig)
            .then(res => {
                resolve(res)
            })
            .catch(error => {
                reject(error)
            })
    })

}


export default myAxios